//
//  MyTableViewCell.m
//  HomeWork-22
//
//  Created by 鹏飞罗 on 16/8/13.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "MyTableViewCell.h"
#import "MyCollectionViewCell.h"
#import "List.h"
#import "Mvlist.h"
#define kCollectionViewCell @"cell"
@interface MyTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *mvListArray;
@end

@implementation MyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}
-(void) createSubviews {
    self.mvListArray = [NSMutableArray array];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 10;
    
    _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_myCollectionView];
    [_myCollectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCollectionViewCell];
}
- (void)setList:(List *)list {
    _list = list;
     self.mvListArray = [NSMutableArray arrayWithArray:_list.mvlist];
}
- (void)setMvListArray:(NSMutableArray *)mvListArray {
    _mvListArray = mvListArray;
    [self.myCollectionView reloadData];
}
// 设置不同items 的宽和高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = CGRectGetWidth(self.contentView.bounds);
    CGFloat height = CGRectGetHeight(self.contentView.bounds);
    if (indexPath.row == 0) {
        return CGSizeMake(width, height / 2);
    } else {
        return CGSizeMake((width - 10) / 2, height / 4);
    }
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _myCollectionView.frame = CGRectMake(0, 0, CGRectGetWidth(self.contentView.bounds), CGRectGetHeight(self.contentView.bounds));
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.mvListArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCell forIndexPath:indexPath];
    Mvlist *mvList = _mvListArray[indexPath.row];
    if (mvList.picurl.length != 0) {
        cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mvList.picurl]]];
    } else {
        NSString *imageURL = [NSString stringWithFormat:@"http://shp.qpic.cn/qqvideo_ori/0/%@_360_204/0?max_age=259200", mvList.vid];
        cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
    }
    
    NSString *musicAndSingerString = [mvList.singer_name stringByAppendingString:mvList.mvtitle];
    cell.musicAndSingerLabel.text = musicAndSingerString;
    cell.detailLabel.text = mvList.mvdesc;
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
